#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/wait.h>

int main(){

    // ps -ef | grep pipe
    
    int pipefd[2];
    int ret=pipe(pipefd);
    if(ret<0){
        perror("pipe perror");
        return -1;
    }
    pid_t pid1=fork();
    if(pid1==0){
        //ps 将标准输出重定向到管道写入端
        close(pipefd[0]);
        dup2(pipefd[1],1);
        execlp("ps","ps","-ef",NULL);
        exit(0);
    }
    pid_t pid2=fork();
    if(pid2==0){
        //grep 将标准输入重定向到管道读取端
        close(pipefd[1]);
        dup2(pipefd[0],0);
        execlp("grep","grep","pipe",NULL);
        exit(0);
    }
    close(pipefd[0]);
    close(pipefd[1]);
    wait(NULL);
    return 0;
}
